<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>订单生成测试</title>
    <style>
        body {
            font-family: 'Helvetica Neue', Arial, sans-serif;
            line-height: 1.6;
            padding: 20px;
            max-width: 800px;
            margin: 0 auto;
            color: #333;
        }
        h1 {
            text-align: center;
            margin-bottom: 30px;
            color: #1a73e8;
        }
        .form-container {
            background-color: #f5f5f5;
            border-radius: 8px;
            padding: 20px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        input, textarea {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
            font-size: 14px;
        }
        .book-item {
            background-color: #fff;
            border: 1px solid #ddd;
            border-radius: 4px;
            padding: 15px;
            margin-bottom: 15px;
            position: relative;
        }
        .remove-book {
            position: absolute;
            top: 10px;
            right: 10px;
            background-color: #ff4d4f;
            color: white;
            border: none;
            border-radius: 4px;
            padding: 5px 10px;
            cursor: pointer;
        }
        .add-book, .submit-btn {
            background-color: #1a73e8;
            color: white;
            border: none;
            border-radius: 4px;
            padding: 10px 15px;
            cursor: pointer;
            font-size: 14px;
            margin-top: 10px;
        }
        .add-book:hover, .submit-btn:hover {
            background-color: #155cbf;
        }
        .book-list {
            margin-top: 20px;
        }
        .response-container {
            margin-top: 30px;
            background-color: #f0f7ff;
            border-radius: 8px;
            padding: 20px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }
        .response-heading {
            margin-top: 0;
            color: #1a73e8;
            border-bottom: 1px solid #ddd;
            padding-bottom: 10px;
        }
        pre {
            background-color: #f8f8f8;
            padding: 15px;
            border-radius: 4px;
            overflow-x: auto;
        }
        .token-container {
            margin-bottom: 20px;
        }
    </style>
</head>
<body>
    <h1>订单生成测试</h1>
    
    <div class="token-container">
        <div class="form-group">
            <label for="token">授权Token:</label>
            <input type="text" id="token" placeholder="请输入有效的JWT Token">
        </div>
    </div>
    
    <div class="form-container">
        <div class="form-group">
            <label for="remark">订单备注:</label>
            <textarea id="remark" rows="3" placeholder="请输入订单备注信息（可选）"></textarea>
        </div>
        
        <div class="book-list" id="bookList">
            <!-- 书籍列表将动态添加 -->
        </div>
        
        <button type="button" class="add-book" id="addBook">添加书籍</button>
        <button type="button" class="submit-btn" id="generateOrder">生成订单</button>
    </div>
    
    <div class="response-container" id="responseContainer" style="display: none;">
        <h2 class="response-heading">接口响应结果</h2>
        <pre id="responseOutput"></pre>
    </div>
    
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const bookList = document.getElementById('bookList');
            const addBookBtn = document.getElementById('addBook');
            const generateOrderBtn = document.getElementById('generateOrder');
            const responseContainer = document.getElementById('responseContainer');
            const responseOutput = document.getElementById('responseOutput');
            
            // 添加第一本书
            addBookItem();
            
            // 添加书籍按钮事件
            addBookBtn.addEventListener('click', function() {
                addBookItem();
            });
            
            // 生成订单按钮事件
            generateOrderBtn.addEventListener('click', function() {
                generateOrder();
            });
            
            // 添加书籍项
            function addBookItem() {
                const bookItem = document.createElement('div');
                bookItem.className = 'book-item';
                bookItem.innerHTML = `
                    <button type="button" class="remove-book">删除</button>
                    <div class="form-group">
                        <label>书籍ID:</label>
                        <input type="number" class="book-id" placeholder="请输入书籍ID" required>
                    </div>
                `;
                
                bookList.appendChild(bookItem);
                
                // 删除书籍按钮事件
                bookItem.querySelector('.remove-book').addEventListener('click', function() {
                    bookList.removeChild(bookItem);
                });
            }
            
            // 生成订单
            function generateOrder() {
                const token = document.getElementById('token').value.trim();
                if (!token) {
                    alert('请输入有效的JWT Token');
                    return;
                }
                
                const bookItems = document.querySelectorAll('.book-item');
                if (bookItems.length === 0) {
                    alert('请至少添加一本书籍');
                    return;
                }
                
                const books = [];
                let isValid = true;
                
                bookItems.forEach(item => {
                    const bookId = item.querySelector('.book-id').value;
                    if (!bookId) {
                        alert('书籍ID不能为空');
                        isValid = false;
                        return;
                    }
                    
                    books.push({
                        book_id: parseInt(bookId)
                    });
                });
                
                if (!isValid) return;
                
                const orderData = {
                    books: books,
                    remark: document.getElementById('remark').value
                };
                
                // 显示loading状态
                responseContainer.style.display = 'block';
                responseOutput.textContent = '请求中...';
                
                // 检查是否在文件系统中直接打开
                if (window.location.protocol === 'file:') {
                    responseOutput.textContent = '错误: 无法直接从文件系统访问API。\n\n请通过Spring Boot应用访问此页面:\nhttp://localhost:8080/test/order\n\n或者启动本地开发服务器。';
                    return;
                }
                
                // 发送请求
                fetch('/api/orders/generate', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        'Authorization': token
                    },
                    body: JSON.stringify(orderData)
                })
                .then(response => {
                    if (!response.ok) {
                        throw new Error(`HTTP错误 ${response.status}: ${response.statusText}`);
                    }
                    return response.json();
                })
                .then(data => {
                    responseOutput.textContent = JSON.stringify(data, null, 2);
                })
                .catch(error => {
                    responseOutput.textContent = '请求错误: ' + error.message;
                });
            }
        });
    </script>
</body>
</html> 